package com.agriculture.mapper;

import com.agriculture.domain.OrderItem;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface OrderItemMapper {
    @Insert("INSERT INTO OrderItems(order_id, product_id, quantity, unit_price) " +
            "VALUES(#{orderId}, #{productId}, #{quantity}, #{unitPrice})")
    @Options(useGeneratedKeys = true, keyProperty = "itemId")
    int insert(OrderItem orderItem);

    @Select("SELECT oi.*, p.product_name, p.price, p.unit " +
           "FROM OrderItems oi " +
           "LEFT JOIN Products p ON oi.product_id = p.product_id " +
           "WHERE oi.order_id = #{orderId}")
    @Results({
        @Result(property = "itemId", column = "item_id"),
        @Result(property = "orderId", column = "order_id"),
        @Result(property = "productId", column = "product_id"),
        @Result(property = "quantity", column = "quantity"),
        @Result(property = "unitPrice", column = "unit_price")
    })
    List<OrderItem> findByOrderId(Long orderId);

    @Select("SELECT * FROM OrderItems WHERE item_id = #{itemId}")
    OrderItem findById(Long itemId);

    @Delete("DELETE FROM OrderItems WHERE order_id = #{orderId}")
    int deleteByOrderId(Long orderId);
} 